package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.ZQUser.CreateZQOneProjectActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.GetPwdOneActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.PhoneLoginActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.LoginInfo;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.linkage.jz.bankruptcy.bean.TokenInfo;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.SignUtil;
import com.linkage.jz.bankruptcy.widget.PublicCreateProLongNoticeDialog;
import com.linkage.jz.bankruptcy.widget.PublicLongNoticeDialog;
import com.linkage.jz.bankruptcy.widget.PublicProLongNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.activity.chat.SearchProjectActivity;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * A login screen that offers login via email/password.
 */
public  class CreateProjectActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = CreateProjectActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog ;
    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
//        btn_login.setOnClickListener(this);
        top_back.setOnClickListener(this);
//        ll_phonelogin.setOnClickListener(this);
//        getPwd.setOnClickListener(this);
        ll_charger.setOnClickListener(this);
        ll_admin.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        top_title.setText("选择角色身份");
        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }

    ImageView mLeftLogo;
    ImageView iv_glz,iv_fzr;
    EditText et_username;
    EditText et_password;
    Button btn_login;
    TextView top_title,getPwd;
    Button top_back,top_mune;
    CheckBox ck_remember;
    LinearLayout ll_charger,ll_admin,qqlogin;
    public static void start(Context context) {
        Intent intent = new Intent(context, CreateProjectActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_create_pro);

        initview();

//        String user = SPUtils.getInstance(Consts.SPNAME).getString("loginUser");
//        String pwd = SPUtils.getInstance(Consts.SPNAME).getString("loginPwd");
//
//        if(!user.equalsIgnoreCase(""))
//        {
//            et_username.setText(user);
//            et_password.setText(pwd);
//        }
        shapeLoadingDialog = new ShapeLoadingDialog(this);

        if(BankruptcyApp.loginInfo !=null) {
            String data = SPUtils.getInstance(Consts.SPNAME).getString("returnFlag1" + BankruptcyApp.loginInfo.getId());
            if ("".equalsIgnoreCase(data)) {
                getData();
            }
        }else{
            finish();
        }
//        noticelxDialog = new PublicProLongNoticeDialog(CreateProjectActivity.this, onClickListener,
//                3, "", "",
//                "", "我知道了");
//        noticelxDialog.show();
        super.onCreate(savedInstanceState);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层


        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        top_back = findViewById(R.id.top_back);
        top_mune = findViewById(R.id.top_mune);
        ll_charger = findViewById(R.id.ll_charger);
        getPwd = findViewById(R.id.getPwd);
        ck_remember = findViewById(R.id.ck_remember);
        ll_admin = findViewById(R.id.ll_admin);

        iv_glz = findViewById(R.id.iv_glz);
        iv_fzr = findViewById(R.id.iv_fzr);

        iv_glz.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        iv_glz.setBackgroundResource(R.drawable.create_pro_selected);
                        iv_fzr.setBackgroundResource(R.drawable.create_pro_normal);
                        break;
                    case MotionEvent.ACTION_UP:
                        MyProInfo info = new MyProInfo();
                        info.setAddRole("0");
                        info.setManagerListStr("[]");
                        CreateProjectDetailActivity.start(CreateProjectActivity.this,info,1);
                        finish();
                        break;

                }
                return true;
            }
        });

        iv_fzr.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        iv_glz.setBackgroundResource(R.drawable.create_pro_normal);
                        iv_fzr.setBackgroundResource(R.drawable.create_pro_selected);
                        break;
                    case MotionEvent.ACTION_UP:
                        MyProInfo info1 = new MyProInfo();
                        info1.setAddRole("1");
                        info1.setManagerListStr("[]");
                        CreateProjectDetailActivity.start(CreateProjectActivity.this,info1,1);
                        finish();
                        break;
                }
                return true;
            }
        });
    }



    @Override
    public void onClick(View view) {

            String phone = "";
            String code = "";
            String pwd = "";
            switch (view.getId()) {
                case R.id.btn_login:
                    //返回
//                MainActivity.start(this);
//                finish();
                    phone = et_username.getText().toString();
//                    code = et_vcode.getText().toString();
                    pwd = et_password.getText().toString();
                    if ("".equalsIgnoreCase(phone)) {
                        Toast.makeText(this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if ("".equalsIgnoreCase(pwd)) {
                        Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
//                    LoginUser(phone, code, pwd);
                    break;
                case R.id.ll_admin:
                    MyProInfo info = new MyProInfo();
                    info.setAddRole("0");
                    CreateProjectDetailActivity.start(this,info,1);
                    finish();
                    break;
                case R.id.ll_charger:
                    MyProInfo info1 = new MyProInfo();
                    info1.setAddRole("1");
                    CreateProjectDetailActivity.start(this,info1,1);
                    finish();
                    break;
                case R.id.top_back:
                    finish();
                    break;
                case R.id.getPwd:
                    GetPwdOneActivity.start(this);
                    break;
                case R.id.wxlogin:


                    break;
                default:
                    break;
            }


    }


    private PublicCreateProLongNoticeDialog noticelxDialog;

    private void getData() {


        shapeLoadingDialog.setLoadingText("请求信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();

        Observable<JsonObject> observable;
            observable = Network
                    .getAPIService()
                    .checkCreaeProject(getUserToken());


        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        shapeLoadingDialog.dismiss();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        JsonObject data = json.get("data").getAsJsonObject();
                        Boolean flag = data.get("returnFlag").getAsBoolean();
                        if(!flag)
                        {
                            noticelxDialog = new PublicCreateProLongNoticeDialog(CreateProjectActivity.this, onClickListener,
                                    3, "", "",
                                    "", "");
                            noticelxDialog.show();
                        }
                        SPUtils.getInstance(Consts.SPNAME).put("returnFlag1"+BankruptcyApp.loginInfo.getId(), "1");
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        shapeLoadingDialog.dismiss();
                        super.onError(e);
                    }
                });

    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                case R.id.ll_close:
                    noticelxDialog.dismiss();
                    break;
                case R.id.btn_chancel_notice:
                    noticelxDialog.dismiss();

                    break;
                case R.id.ll_close_lx:
                case R.id.btn_chancel_notice_lx:
                    noticelxDialog.dismiss();
                    break;

                case R.id.btn_save_notice_lx:
                    noticelxDialog.dismiss();
                    break;

            }
        }
    };



}

